package com.android.djlmapp.adapter.my;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;

public abstract class MyBaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<MyBaseRecyclerViewAdapter.ViewHolder> {
    protected List<T> tList;
    protected Context mContext;
    protected LayoutInflater mLayoutInflater;
    protected int layId;

    private OnItemClickListener mOnItemClickListener;

    public MyBaseRecyclerViewAdapter(List<T> tList, Context mContext, int layId) {
        this.tList = tList;
        this.mContext = mContext;
        this.layId=layId;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(layId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final MyBaseRecyclerViewAdapter.ViewHolder holder, final int position) {
        convert(holder,tList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(position);
            }
        });
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;

    }

    @Override
    public int getItemCount() {
        return tList.size();
    }

    public abstract  void convert(ViewHolder holder,T itemData);

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
    /**
     * 多布局接口
     * @param <T>
     */
    public interface  MultiTypeSupport<T>{
        int getLayoutId(T item);
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> mViewArray;

        public ViewHolder(View itemView) {
            super(itemView);
            mViewArray = new SparseArray<>();
        }

        /**
         * 通过id得到view
         * @param viewId
         * @param <V>
         * @return
         */
        public <V extends View>V getViewAtId(int viewId){
            View view = mViewArray.get(viewId);
            if(view==null){
                view=itemView.findViewById(viewId);
                mViewArray.put(viewId,view);
            }
            return (V)view;
        }

        /**
         * 设置文字
         * @param viewId
         * @param text
         * @return
         */
        public ViewHolder setText(int viewId,CharSequence text){
            TextView textView= getViewAtId(viewId);
            textView.setText(text);
            return  this;
        }

        /**
         * 通过网址设置图片
         * @param viewId
         * @param imgUrl
         * @return
         */
        public ViewHolder setImageFromUrl(int viewId,String imgUrl){
            ImageView imageView= getViewAtId(viewId);
            Glide.with(imageView.getContext()).load(imgUrl).into(imageView);
            return this;
        }

        /**
         * 通过资源来设置图片资源
         * @param viewId
         * @param resId
         * @return
         */
        public ViewHolder setImageFromRes(int viewId,int resId){
            ImageView imageView = getViewAtId(viewId);
            imageView.setImageResource(resId);
            return this;
        }

        /**
         * 设置控件透明
         * @param viewId
         * @param visibility
         * @return
         */
        public ViewHolder setViewVisibility(int viewId,int visibility){
            View view = getViewAtId(viewId);
            view.setVisibility(visibility);
            return this;
        }

        public ViewHolder setSwitchStatus(int viewId,boolean boo){
            Switch view = getViewAtId(viewId);
            view.setChecked(boo);
            return this;
        }

        public ViewHolder setSwitchClick(int viewId,boolean boo){
            Switch view = getViewAtId(viewId);
            view.setClickable(boo);
            return this;
        }
    }





}

